package org.example;

import java.util.ArrayList;
import java.util.List;

public class Test3 {
    //leetcode 131 分隔回文串 https://leetcode.cn/problems/palindrome-partitioning/description/
    public List<List<String>> ret;
    public List<String> list;
    public List<List<String>> partition(String s) {
        ret = new ArrayList<>();
        list = new ArrayList<>();
        backtracking(s,0);
        return ret;
    }

    public void backtracking(String s,int start) {
        if (start >= s.length()) {
            ret.add(new ArrayList<>(list));
            return;
        }
        for (int i = start; i < s.length(); i++) {
            String tmp = s.substring(start,i + 1);
            if (isPalindrome(tmp)) {
                list.add(tmp);
            }else {
                continue;
            }
            backtracking(s,i + 1);
            list.remove(list.size() - 1);
        }
    }

    private boolean isPalindrome(String s) {
        for (int i = 0, j = s.length() - 1; i < j; i++, j--) {
            if (s.charAt(i) != s.charAt(j)) {
                return false;
            }
        }
        return true;
    }
}
